package com.ruoyi.common.utils.poi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;

import java.io.File;
import java.util.Date;

/***
 * 自定义生成一些excel表格
 */
public class HutoolExcelUtiles {
    private static final String XLS = ".xls";
    private static final String XLSX = ".xlsx";

    public static File RecordFileExport(JSONArray jsonArray) {
        if (CollUtil.isEmpty(jsonArray)) {
            throw new GlobalException("没有要导出的数据");
        }
        String filename = "文件级-" + DateUtils.dateTimeNow() + XLS;
        File file = new File(RuoYiConfig.getDownloadPath() + filename);
        ExcelWriter fw = new ExcelWriter(file);
        // 设置表头
        anjuanExport_ExcelHeader(fw);
        // 生成文件
        int y = 1, num = 1, x;
        JSONObject json = null;
        Date updateTime;
        for (int i = 0; i < jsonArray.size(); i++) {
            //  System.out.println(jsonArray.get(i));
            json = jsonArray.getJSONObject(i);
            x = 0;
            fw.writeCellValue(x++, y, num);
            fw.writeCellValue(x++, y, json.getJSONObject("archives").getString("caseFileTitle"));
            fw.writeCellValue(x++, y, json.getString("sequenceNum"));
            fw.writeCellValue(x++, y, json.getString("fileNo"));
            fw.writeCellValue(x++, y, json.getString("docTitle"));
            fw.writeCellValue(x++, y, json.getString("objStr1"));
            fw.writeCellValue(x++, y, json.getString("objStr2"));
            fw.writeCellValue(x++, y, json.getString("objStr3"));
            fw.writeCellValue(x++, y, json.getString("objStr4"));
            fw.writeCellValue(x++, y, json.getString("pageCounts"));
            fw.writeCellValue(x++, y, json.getString("pageNo"));
            fw.writeCellValue(x++, y, json.getString("remark"));
            fw.writeCellValue(x++, y, DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", json.getDate("createTime")));
            updateTime = json.getDate("updateTime");
            if (ObjectUtil.isEmpty(updateTime)) {
                fw.writeCellValue(x++, y, "");
            } else {
                fw.writeCellValue(x++, y, DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", updateTime));
            }
            fw.autoSizeColumn(y);
            y++;
            num++;
        }
        fw.flush();
        return file;
    }

    private static void anjuanExport_ExcelHeader(ExcelWriter writer) {
        int x = 0;
        writer.writeCellValue(x++, 0, "序号");
        writer.writeCellValue(x++, 0, "案卷题名");
        writer.writeCellValue(x++, 0, "顺序号");
        writer.writeCellValue(x++, 0, "文件编号");
        writer.writeCellValue(x++, 0, "文件题名");
        writer.writeCellValue(x++, 0, "责任者");
        writer.writeCellValue(x++, 0, "档号");
        writer.writeCellValue(x++, 0, "文图号");
        writer.writeCellValue(x++, 0, "载体类型");
        writer.writeCellValue(x++, 0, "页数");
        writer.writeCellValue(x++, 0, "页号");
        writer.writeCellValue(x++, 0, "备注");
        writer.writeCellValue(x++, 0, "创建时间");
        writer.writeCellValue(x++, 0, "更新时间");
    }
}
